package com.richapp.launcher.control;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.provider.Settings;
public class CustomSysSettingManager {
	private Context context;
	private AudioManager mAudioManager;
	
	private int minBrightness = 20;
	private int maxBrightness = 255;
	
	private static final int MIN_INPUT_BRIGHTNESS=0;
	private static final int MIN_INPUT_VOLUME=0;
	
	private static final int MAX_INPUT_BRIGHTNESS=100;
	private static final int MAX_INPUT_VOLUME=100;

	public static enum SoundType{
		System,Ring,Call,Alerm,Music
	}

	public CustomSysSettingManager(Context context) {
		this.context=context;
		mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
	}
	
	/**
	 * 获取最大音量
	 * @param type
	 * @return
	 */
	private int getMaxVolume(SoundType type){
		int maxVolume = -1;
		if(type.equals(SoundType.System)){
			maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
		}else if(type.equals(SoundType.Music)){
			maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		}else if(type.equals(SoundType.Call)){
			maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
		}else if(type.equals(SoundType.Ring)){
			maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
		}else if(type.equals(SoundType.Alerm)){
			maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
		}
		return maxVolume;
	}
	
	/**
	 * 获取当前音量
	 * @param type
	 * @return
	 */
	public int getCurrentVolume(SoundType type){
		int currentVolume = -1;
		int max = -1;
		if(type.equals(SoundType.System)){
			max = getMaxVolume(SoundType.System);
			currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM)* MAX_INPUT_VOLUME/max ;
		}else if(type.equals(SoundType.Music)){
			max = getMaxVolume(SoundType.Music);
			currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)* MAX_INPUT_VOLUME/max ;
		}else if(type.equals(SoundType.Call)){
			max = getMaxVolume(SoundType.Call);
			currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) * MAX_INPUT_VOLUME/max;
		}else if(type.equals(SoundType.Ring)){
			max = getMaxVolume(SoundType.Ring);
			currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING)* MAX_INPUT_VOLUME/max ;
		}else if(type.equals(SoundType.Alerm)){
			max = getMaxVolume(SoundType.Alerm);
			currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM) * MAX_INPUT_VOLUME/max;
		}
		return currentVolume;
	}
	
	/**
	 * 设置当前音量
	 * @param type
	 * @param volume
	 */
	public void setVolume(SoundType type,int volume){
		if(volume>MAX_INPUT_VOLUME || volume<MIN_INPUT_VOLUME)return;
		int volumeValue = -1;
		
		if(type.equals(SoundType.System)){
			volumeValue = volume*getMaxVolume(SoundType.System)/MAX_INPUT_VOLUME;
			mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, volumeValue, 0);
		}else if(type.equals(SoundType.Music)){
			volumeValue = volume*getMaxVolume(SoundType.Music)/MAX_INPUT_VOLUME;
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeValue, 0);
		}else if(type.equals(SoundType.Call)){
			volumeValue = volume*getMaxVolume(SoundType.Call)/MAX_INPUT_VOLUME;
			mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, volumeValue, 0);
		}else if(type.equals(SoundType.Ring)){
			volumeValue = volume*getMaxVolume(SoundType.Ring)/MAX_INPUT_VOLUME;
			mAudioManager.setStreamVolume(AudioManager.STREAM_RING, volumeValue, 0);
		}else if(type.equals(SoundType.Alerm)){
			volumeValue = volume*getMaxVolume(SoundType.Alerm)/MAX_INPUT_VOLUME;
			mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, volumeValue, 0);
		}
	}

	/**
	 * 判断是否在播放音乐或者播放视频
	 * @return true/false
	 */
	public boolean isAudioOrMediaPlay(){
		return mAudioManager.isMusicActive();
	}
	
	/**
	 * 获取当前亮度
	 * @return
	 */
	public int getCurrentBrightness()throws Exception{
		int brightness = -1;
		ContentResolver resolver = context.getContentResolver();
		brightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS)*MAX_INPUT_BRIGHTNESS/255;
		return brightness;
	}
	
	/**
	 * 设置亮度
	 * @param inputValue 1~100
	 * @throws Exception
	 */
	public void setScreenBrightness(int inputValue)throws Exception{
		if(inputValue<MIN_INPUT_BRIGHTNESS || inputValue>MAX_INPUT_BRIGHTNESS)return;
		int brightness = minBrightness + (int)((inputValue/(float)MAX_INPUT_BRIGHTNESS)*(maxBrightness-minBrightness));
		ContentResolver resolver = context.getContentResolver();
		if(Settings.System.getInt(resolver,Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){
			Settings.System.putInt(resolver,Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		}
		Settings.System.putInt(resolver,Settings.System.SCREEN_BRIGHTNESS,brightness);
	}
	
}
